package com.sgb.kjwl.pop;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.PopwindowMainPublishBinding;
import com.sgb.kjwl.utils.ViewUtils;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.kjwl.view.ui.activity.MainActivity;
import com.swgk.core.view.fragment.BigHomeMenuFragment;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by harisucici on 2020/4/28
 * Class description:
 */
public class DialogAddFolder extends DialogFragment {
    public static String TAG = "DIALOG_ADD_FOLDER";
    PopwindowMainPublishBinding mBinding;
    private OnDialogListener mlistener;


    private RecyclerView rl_rv_menu;
    private TextView tv_cancel;
    private FragPagerAdapter menuAdapter;
    private MainPublishDialog.DialogClickCallBack dialogClickCallBack;
    private ArrayList<Fragment> menuFragments;
    private int page;
    public static final int COLUMN_COUNT = 5;
    public static final int ROW_COUNT = 2;
    private MainActivity mMainActivity;
    private ViewPager viewpager;
    private ImageView[] memuTip;
    private LinearLayout llViewpager;
    private LinearLayout dotMenuLayout;

    public static DialogAddFolder newInstance(List<MenuEntity> data) {
        DialogAddFolder fragment = new DialogAddFolder();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", (Serializable) data);
        fragment.setArguments(bundle);
        return fragment;
    }

    public void setOnDialogListener(OnDialogListener dialogListener) {
        this.mlistener = dialogListener;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(params);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.popwindow_main_publish, container, false);
        ViewUtils.slideToDown(mBinding.getRoot());
        mBinding.setEvent(new EventListener());
        return mBinding.getRoot();
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    public interface OnDialogListener {
        void onDialogClick(String tag, String inputText);
    }

    public class EventListener extends DialogAddFolder {

        public void onUpload(View view) {
            mlistener.onDialogClick(TAG, "upload");
            dismiss();
        }

        public void onNew(View view) {
            mlistener.onDialogClick(TAG, "new");
            dismiss();
        }
    }


    protected void initView(View view) {
//        rl_rv_menu = holder.getDialog().findViewById(R.id.rv_menu);
        tv_cancel = view.findViewById(R.id.img_close);
        viewpager = view.findViewById(R.id.viewpager);
        llViewpager = view.findViewById(R.id.ll_viewpager);
        dotMenuLayout = view.findViewById(R.id.dot_menu_layout);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
//        menuAdapter = new MenuPublishPopAdapter(context, null);
//        rl_rv_menu.setLayoutManager(new GridLayoutManager(context, 5));
//        rl_rv_menu.setAdapter(menuAdapter);
//        menuAdapter.setOnItemClick(new PageRecycleAdapter.OnItemClick<MenuEntity>() {
//            @Override
//            public void onClick(MenuEntity menuEntity, int position) {
//                dialogClickCallBack.onItemClick(menuEntity.getId());
//            }
//        });
        menuAdapter = new FragPagerAdapter(getChildFragmentManager(), new ArrayList<>());
        viewpager.setAdapter(menuAdapter);
        viewpager.setOffscreenPageLimit(page);
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setImageBackgroundMenu(position % page);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        initDotsMenu();
        llViewpager.setVisibility(View.VISIBLE);
        List<MenuEntity> menuData = (List<MenuEntity>) getArguments().getSerializable("data");
        showMenuView(menuData);
    }


    public void showMenuView(List<MenuEntity> data) {
        if (menuFragments == null)
            menuFragments = new ArrayList<>();
        menuFragments.clear();
        int total = data.size();
        page = total % (COLUMN_COUNT * ROW_COUNT) > 0 ? total / (COLUMN_COUNT * ROW_COUNT) + 1 : total / (COLUMN_COUNT * ROW_COUNT);
        for (int i = 0; i < page; i++) {
            List<MenuEntity> menuEntityList = new ArrayList<>();
            int fir = i * COLUMN_COUNT * ROW_COUNT;
            int last = (i + 1) * COLUMN_COUNT * ROW_COUNT > total ? total : (i + 1) * COLUMN_COUNT * ROW_COUNT;
            menuEntityList.addAll(data.subList(fir, last));
            menuFragments.add(BigHomeMenuFragment.newInstance(menuEntityList, COLUMN_COUNT, ROW_COUNT, i));
            BigHomeMenuFragment big = (BigHomeMenuFragment) menuFragments.get(i);
            big.setOnItemClickCallBack(new BigHomeMenuFragment.OnItemClickCallBack() {
                @Override
                public void onClick(MenuEntity entity, int position,int num) {
                }
            });
        }
        menuAdapter.setFragments(menuFragments);
    }

    private void setImageBackgroundMenu(int selectItems) {
        for (int i = 0; i < memuTip.length; i++) {
            if (i == selectItems) {
                memuTip[i].setSelected(true);
            } else {
                memuTip[i].setSelected(false);
            }
        }
    }

    private void initDotsMenu() {
        dotMenuLayout.removeAllViews();
        memuTip = new ImageView[page];
        for (int i = 0; i < page; i++) {
            ImageView imageView = new ImageView(this.mMainActivity);
            imageView.setBackgroundResource(R.drawable.select_banner_dot);
            memuTip[i] = imageView;
            if (i == 0) {
                memuTip[i].setSelected(true);
            } else {
                memuTip[i].setSelected(false);
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams((int) mMainActivity.getResources().getDimension(R.dimen.dp8), (int) mMainActivity.getResources().getDimension(R.dimen.dp8)));
            layoutParams.leftMargin = (int) mMainActivity.getResources().getDimension(R.dimen.dp5);
            layoutParams.rightMargin = (int) mMainActivity.getResources().getDimension(R.dimen.dp5);
            dotMenuLayout.addView(imageView, layoutParams);
        }
    }



}